package com.zoe.thread.th14_volatile测试;

import java.util.concurrent.TimeUnit;

/**
 * @Author Zoe
 * @Date: 2021/05/06
 * @Description
 */
public class JMMDemo {
    public static volatile int num = 0;

    public static void main(String[] args) {
        System.out.println("main-start");
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ": start");
            num = 1;
        }, "线程A").start();

        new Thread(() -> {
            while (true) {
                if (num == 1) {
                    System.out.println("num发生变化");
                    break;
                }
            }
        }, "B").start();

        System.out.println(Thread.currentThread().getName() + "-end: " + num);

    }
}
